﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WGM_Client_Net35
{
    /// <summary>
    /// Interaction logic for AdminWindow.xaml
    /// </summary>
    public partial class AdminWindow : Window
    {
        private MainWindow _parent;
        public AdminWindow(MainWindow parent)
        {
            _parent = parent;
            
            InitializeComponent();
            _onlineNameChooser.ItemsSource = _parent.ConnectionHandler.OnlineUsers;
            if (_parent.ConnectionHandler.OnlineUsers.Count == 0)
            {
                _onlineNameChooser.IsEnabled = false;
                LogOffBtn.IsEnabled = false;
            }
            else
            {
                _onlineNameChooser.SelectedIndex = 0;
            }

            
        }



        private void LogOffBtn_MouseDown(object sender, MouseButtonEventArgs e)
        {
            KickClient(_onlineNameChooser.Text);

        }
        private void KickClient(string userName)
        {
            WGM_Service.WGMMessage logOffMessage = new WGM_Client_Net35.WGM_Service.WGMMessage();
            logOffMessage.SenderLogon = userName as string;
            logOffMessage.ReceiverLogon = _parent.ConnectionHandler.CurrentUserName;
            logOffMessage.MessageType = WGM_Client_Net35.WGM_Service.MessageTypes.USER_LEAVING;
            logOffMessage.Message = _logOffUserReasonTB.Text.Trim() == "" ? "Server is being upgraded." : _logOffUserReasonTB.Text;
            logOffMessage.TimeStamp = DateTime.Now;

            _parent.ConnectionHandler.SendMessage(logOffMessage);
        }
        private void ClearServerBtn_MouseDown(object sender, MouseButtonEventArgs e)
        {
            foreach (string user in _onlineNameChooser.Items)
            {
                if (user.ToLower().Trim() == _parent.ConnectionHandler.CurrentUserName.ToLower().Trim())
                    continue;
                KickClient(user);
            }
        }
    }
}
